<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="dataForm.taskName" placeholder="任务名称"></el-input>
      </el-form-item>
          <el-form-item label="采买订单类型0:app，1:小程序，2:cpl ,3:快应用" prop="adType">
          <el-input v-model="dataForm.adType" placeholder="采买订单类型0:app，1:小程序，2:cpl ,3:快应用"></el-input>
      </el-form-item>
          <el-form-item label="任务状态，1:开启，2:关闭" prop="status">
          <el-input v-model="dataForm.status" placeholder="任务状态，1:开启，2:关闭"></el-input>
      </el-form-item>
          <el-form-item label="落地页地址adType为2时返回，adType 为3时返回快
应用落地页地址" prop="landingPageUrl">
          <el-input v-model="dataForm.landingPageUrl" placeholder="落地页地址adType为2时返回，adType 为3时返回快
应用落地页地址"></el-input>
      </el-form-item>
          <el-form-item label="产品名称" prop="productName">
          <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
      </el-form-item>
          <el-form-item label="包名" prop="pkgName">
          <el-input v-model="dataForm.pkgName" placeholder="包名"></el-input>
      </el-form-item>
          <el-form-item label="投放app安卓版下载地址， adType为0时返回" prop="appUrl">
          <el-input v-model="dataForm.appUrl" placeholder="投放app安卓版下载地址， adType为0时返回"></el-input>
      </el-form-item>
          <el-form-item label="投放快应用地址，adType 为3时返回" prop="fastAppUrl">
          <el-input v-model="dataForm.fastAppUrl" placeholder="投放快应用地址，adType 为3时返回"></el-input>
      </el-form-item>
          <el-form-item label="快应用客户名称 adType 为3时返回" prop="fastAppName">
          <el-input v-model="dataForm.fastAppName" placeholder="快应用客户名称 adType 为3时返回"></el-input>
      </el-form-item>
          <el-form-item label="是否需要绑定 RTA，1:绑定， 2:不绑定" prop="isRta">
          <el-input v-model="dataForm.isRta" placeholder="是否需要绑定 RTA，1:绑定， 2:不绑定"></el-input>
      </el-form-item>
          <el-form-item label="RTA请求地址（当isRta = 1时必填）" prop="rtaurl">
          <el-input v-model="dataForm.rtaurl" placeholder="RTA请求地址（当isRta = 1时必填）"></el-input>
      </el-form-item>
          <el-form-item label="结算事件类型，1:激活 2:下单 3:注册 4:付费 7:次日留
存 8:唤起 9: 表单提交" prop="settleType">
          <el-input v-model="dataForm.settleType" placeholder="结算事件类型，1:激活 2:下单 3:注册 4:付费 7:次日留
存 8:唤起 9: 表单提交"></el-input>
      </el-form-item>
          <el-form-item label="预算量级限制 （天） ，描述当⽇要多少个结算， 支持
向上和向下调整" prop="budgeOrderLimit">
          <el-input v-model="dataForm.budgeOrderLimit" placeholder="预算量级限制 （天） ，描述当⽇要多少个结算， 支持
向上和向下调整"></el-input>
      </el-form-item>
          <el-form-item label="结算单价，精确到分。示例：123(表示1.23元)， 2357
（表示23.57）" prop="price">
          <el-input v-model="dataForm.price" placeholder="结算单价，精确到分。示例：123(表示1.23元)， 2357
（表示23.57）"></el-input>
      </el-form-item>
          <el-form-item label="deeplink地址，任务类型为1 时，必填" prop="deeplink">
          <el-input v-model="dataForm.deeplink" placeholder="deeplink地址，任务类型为1 时，必填"></el-input>
      </el-form-item>
          <el-form-item label="策略id， 和监测地址中的fid一致" prop="fid">
          <el-input v-model="dataForm.fid" placeholder="策略id， 和监测地址中的fid一致"></el-input>
      </el-form-item>
          <el-form-item label="曝光地址" prop="impUrls">
          <el-input v-model="dataForm.impUrls" placeholder="曝光地址"></el-input>
      </el-form-item>
          <el-form-item label="点击地址" prop="clkUrls">
          <el-input v-model="dataForm.clkUrls" placeholder="点击地址"></el-input>
      </el-form-item>
          <el-form-item label="定向要求，⽂字描述。 示例：18-50岁，男性，排除偏
远地区" prop="directional">
          <el-input v-model="dataForm.directional" placeholder="定向要求，⽂字描述。 示例：18-50岁，男性，排除偏
远地区"></el-input>
      </el-form-item>
          <el-form-item label="投放时段" prop="daypartingRemark">
          <el-input v-model="dataForm.daypartingRemark" placeholder="投放时段"></el-input>
      </el-form-item>
          <el-form-item label="是否需要开户投放" prop="needCreateAccount">
          <el-input v-model="dataForm.needCreateAccount" placeholder="是否需要开户投放"></el-input>
      </el-form-item>
          <el-form-item label="最后更新时间" prop="updatedAt">
          <el-input v-model="dataForm.updatedAt" placeholder="最后更新时间"></el-input>
      </el-form-item>
          <el-form-item label="流量要求" prop="trafficRequire">
          <el-input v-model="dataForm.trafficRequire" placeholder="流量要求"></el-input>
      </el-form-item>
          <el-form-item label="点击率要求" prop="clickRateRequire">
          <el-input v-model="dataForm.clickRateRequire" placeholder="点击率要求"></el-input>
      </el-form-item>
          <el-form-item label="" prop="cooperationSpec">
          <el-input v-model="dataForm.cooperationSpec" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="wechatName">
          <el-input v-model="dataForm.wechatName" placeholder=""></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        taskName: '',
        adType: '',
        status: '',
        landingPageUrl: '',
        productName: '',
        pkgName: '',
        appUrl: '',
        fastAppUrl: '',
        fastAppName: '',
        isRta: '',
        rtaurl: '',
        settleType: '',
        budgeOrderLimit: '',
        price: '',
        deeplink: '',
        fid: '',
        impUrls: '',
        clkUrls: '',
        directional: '',
        daypartingRemark: '',
        needCreateAccount: '',
        updatedAt: '',
        trafficRequire: '',
        clickRateRequire: '',
        cooperationSpec: '',
        wechatName: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        taskName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        adType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        landingPageUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        pkgName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        appUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fastAppUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fastAppName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isRta: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        rtaurl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        settleType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        budgeOrderLimit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        price: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deeplink: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        impUrls: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        clkUrls: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        directional: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        daypartingRemark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        needCreateAccount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedAt: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        trafficRequire: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        clickRateRequire: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        cooperationSpec: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        wechatName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/adx/sinatask/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/sinatask/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
